package com.example.judgepaper.controller;

import com.example.judgepaper.Dto.ResponseDto.JudgeAnswerPartDto;
import com.example.judgepaper.Dto.ResponseDto.PaperAnswerDto;
import com.example.judgepaper.Dto.ResponseDto.StudentAnswerDto;
import com.example.judgepaper.Dto.ResponseDto.TwoData;
import com.example.judgepaper.service.JudgePaperService;
import com.example.judgepaper.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/judge")
public class JudgePaperController {

    private final JudgePaperService judgePaperService;

    public JudgePaperController(JudgePaperService judgePaperService) {
        this.judgePaperService = judgePaperService;
    }

    @GetMapping(value = "/find")
    public Result<Object> find(@RequestParam("paperid") String id) {
        PaperAnswerDto allMassage = judgePaperService.findAllMassage(id);
        if (allMassage == null) {
            return new Result<>(500, "查无此卷", null);
        } else {
            return new Result<>(200, "查询成功", allMassage.getPaperAnswerPartDto());
        }
    }

    @GetMapping(value = "/stu")
    public Result<Object> findStu(@RequestParam("testid") String testId,
                                  @RequestParam("paperid") String paperId,
                                  @RequestParam("studentid") String studentId) {
        StudentAnswerDto studentResult = judgePaperService
                .findStudentResult(testId, paperId, studentId);
        if (studentResult != null) {
            return new Result<>(200, "查询成功", studentResult.getStudentAnswerPartDto());
        } else
            return new Result<>(501, "学生尚未有成绩", null);
    }

    @GetMapping(value = "/done")
    public Result<Object> judge(@RequestParam("testid") String testId,
                                @RequestParam("paperid") String paperId,
                                @RequestParam("studentid") String studentId) {
        PaperAnswerDto paperAnswerDto = judgePaperService
                .findAllMassage(paperId);
        StudentAnswerDto studentAnswerDto = judgePaperService
                .findStudentResult(testId, paperId, studentId);
        TwoData twoData = judgePaperService
                .studentScoreUpdate(paperAnswerDto, studentAnswerDto);
        if (!twoData.isFlag()) {
            return new Result<>(501, "已经进行过一次自动判卷，这是改后的分数", twoData.getJudgeAnswerPartDto());
        } else if (twoData.isFlag() && twoData.getJudgeAnswerPartDto() != null) {
            return new Result<>(200, "这是第一次判卷,判卷成功", twoData.getJudgeAnswerPartDto());
        } else
            return new Result<>(500, "判卷出错", null);
    }
}
